const {
  aLogin,
  sLogin,
  tLogin,
  //-----
  addStu,
  getStuList,
  delStu,
  delSomeStu,
  updateStu,
  sleSingleStu,
  //------
  addTch,
  getTchList,
  delTch,
  delSomeTch,
  updateTch,
  sleSingleTch,
  //---------
  getStuNumber,
  getTchNumber,
  getExpNumber,
} = require("../controller/user");
const { SuccessModel, ErrorModel } = require("../model/resModel");
var express = require("express");
var router = express.Router();
const jwt = require("jsonwebtoken");
const loginCheck = require("../middleware/loginCheck");
const authCheck = require("../middleware/authCheck");
/* GET users listing. */
router.post("/a_login", function (req, res, next) {
  const { a_name, a_pwd } = req.body;
  const result = aLogin(a_name, a_pwd);
  return result.then((userData) => {
    if (userData.a_name) {
      const token = jwt.sign(
        {
          id: userData.admin_id,
        },
        "#ai_Web"
      );
      userData.token = token;
      res.json(new SuccessModel(userData));
      return;
    }
    res.json(new ErrorModel("登录失败"));
  });
});
router.post("/t_login", function (req, res, next) {
  const { tch_num, tch_pwd } = req.body;
  const result = tLogin(tch_num, tch_pwd);
  return result.then((userData) => {
    if (userData.tch_num) {
      const token = jwt.sign(
        {
          id: userData.tch_id,
        },
        "#ai_Web"
      );
      userData.token = token;
      res.json(new SuccessModel(userData));
      return;
    }
    res.json(new ErrorModel("登录失败"));
  });
});
router.post("/s_login", function (req, res, next) {
  const { stu_num, stu_pwd } = req.body;
  const result = sLogin(stu_num, stu_pwd);
  return result.then((userData) => {
    if (userData.stu_num) {
      const token = jwt.sign(
        {
          id: userData.stu_id,
        },
        "#ai_Web"
      );
      userData.token = token;
      res.json(new SuccessModel(userData));
      return;
    }
    res.json(new ErrorModel("登录失败"));
  });
});

//*********************学生***************************** */
router.post("/add_stu", (req, res, next) => {
  // console.log(req.body);
  const result = addStu(req.body);
  return result.then((val) => {
    if (val) {
      res.json(new SuccessModel());
    } else {
      res.json(new ErrorModel("添加失败"));
    }
  });
});
router.get("/get_stu", function (req, res, next) {
  const result = getStuList();
  return result.then((val) => {
    res.json(new SuccessModel(val));
  });
});
router.get("/del_stu", (req, res, next) => {
  const result = delStu(req.query.id);
  return result.then((val) => {
    if (val) {
      res.json(new SuccessModel(val));
    } else {
      res.json(new ErrorModel("删除失败"));
    }
  });
});
router.post("/del_s_stu", function (req, res, next) {
  const result = delSomeStu(req.body);
  return result.then((val) => {
    if (val) {
      res.json(new SuccessModel());
    } else {
      res.json(new ErrorModel("删除失败"));
    }
  });
});
router.post("/update_stu", function (req, res, next) {
  const result = updateStu(req.query.id, req.body);
  return result.then((val) => {
    if (val) {
      res.json(new SuccessModel());
    } else {
      res.json(new ErrorModel("更新失败"));
    }
  });
});
router.get("/sel_s_stu", (req, res, next) => {
  const result = sleSingleStu(req.query.id);
  return result.then((val) => {
    res.json(new SuccessModel(val));
  });
});
//--------------------教师---------------------------------
router.post("/add_tch", (req, res, next) => {
  // console.log(req.body);
  const result = addTch(req.body);
  return result.then((val) => {
    if (val) {
      res.json(new SuccessModel());
    } else {
      res.json(new ErrorModel("添加失败"));
    }
  });
});
router.get("/get_tch", function (req, res, next) {
  const result = getTchList();
  return result.then((val) => {
    res.json(new SuccessModel(val));
  });
});
router.get("/del_tch", (req, res, next) => {
  const result = delTch(req.query.id);
  // console.log(req.query.id);
  return result.then((val) => {
    if (val) {
      res.json(new SuccessModel(val));
    } else {
      res.json(new ErrorModel("删除失败"));
    }
  });
});
router.post("/del_s_tch", function (req, res, next) {
  const result = delSomeTch(req.body);
  return result.then((val) => {
    if (val) {
      res.json(new SuccessModel());
    } else {
      res.json(new ErrorModel("删除失败"));
    }
  });
});
router.post("/update_tch", function (req, res, next) {
  console.log(req.query.id, req.body);
  const result = updateTch(req.query.id, req.body);
  return result.then((val) => {
    if (val) {
      res.json(new SuccessModel());
    } else {
      res.json(new ErrorModel("更新失败"));
    }
  });
});
router.get("/sel_s_tch", (req, res, next) => {
  const result = sleSingleTch(req.query.id);
  return result.then((val) => {
    res.json(new SuccessModel(val));
  });
});
//********************************************************8 */
router.get("/get_stu_num", function (req, res, next) {
  const result = getStuNumber();
  return result.then((val) => {
    res.json(new SuccessModel(val));
  });
});
router.get("/get_tch_num", function (req, res, next) {
  const result = getTchNumber();
  return result.then((val) => {
    res.json(new SuccessModel(val));
  });
});
router.get("/get_exp_num", function (req, res, next) {
  const result = getExpNumber();
  return result.then((val) => {
    res.json(new SuccessModel(val));
  });
});

module.exports = router;
